﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using com.huigo.Framework.ServiceUtils;
using System.Text;
using com.loto.Common;

namespace com.huigo.Admin.Web.user
{
    public partial class UserList : BasePage
    {
        private int pageIndex
        {
            get
            {
                return com.loto.Common.TypeParse.StrToInt(Request.QueryString["page"], 1);
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        private string queryUserName
        {
            get
            {
                return string.IsNullOrEmpty(Request.QueryString["UserName"]) ? string.Empty : com.loto.Common.Utils.SqlEscape(Request.QueryString["UserName"]);
            }
        }
        protected void pageInfo_PageChanged(object sender, EventArgs e)
        {
            if (!IsPostBack)
                BindData();
        }

        protected void BindData()
        {
            custName.Text = queryUserName;
            List<RequestFuncParams> argList = new List<RequestFuncParams>();
            argList.Add(new RequestFuncParams("userName", queryUserName));
            argList.Add(new RequestFuncParams("pageSize", pageInfo.PageSize.ToString()));
            argList.Add(new RequestFuncParams("pageIndex", pageIndex.ToString()));
            BaseService baseService = new BaseService();
            ParserXml parser = baseService.RequestEntryService(Dict.ERequestFunc.GET_USER_INFO, argList);

            if (parser != null && parser.RowsCount > 0)
            {
                pageInfo.RecordCount = Convert.ToInt16(parser["RecordCount", 0]);
                pageInfo.CurrentPageIndex = pageIndex;
                pageInfo.PageURLFormat = GetPagerUrl();
                StringBuilder sb = new StringBuilder();
                sb.Append("<table><thead><tr><th style=\"width: 10%\">姓名</th><th style=\"width: 10%\">部门</th><th style=\"width: 10%\">登陆账号</th><th style=\"width: 10%\">工号</th><th style=\"width: 20%\">最后登陆时间</th><th style=\"width: 15%\">状态</th><th>操作</th></tr></thead><tbody>");
                string content = "<tr><td>{0}</td><td>{1}</td><td/>{2}</td><td>{3}</td><td>{4}</td><td>{5}</td><td><a href=\"AddUser.aspx?sid={6}&username={2}&keepThis=true&amp;TB_iframe=true&amp;height=500&amp;width=850\" title=\"修改用户- {0}\" class=\"thickbox\">修改</a></td></tr>";
                for (int i = 0; i < parser.RowsCount; i++)
                {
                    string type = com.loto.Common.DictEnum.GetDictValueByKey(Dict.DictIsActive, Utils.StrToInt(parser["UserIsActive", i], int.MinValue));
                    sb.AppendFormat(content, parser["UserRealName", i].ToString(), parser["DeptName", i].ToString(), parser["UserName", i].ToString(), parser["UserNum", i].ToString(), Convert.ToDateTime(parser["UserLastActivedOn", i]).ToString("yyyy-MM-dd HH:mm"), type, parser["UserId", i].ToString());
                }
                sb.Append("</tbody></table>");
                litUserInfo.Text = sb.ToString();
            }
            else
                litUserInfo.Text = string.Empty;


        }

        private string GetPagerUrl()
        {
            string url = "UserList.aspx?";
            url += "page={0}";
            if (queryUserName != string.Empty)
                url += "&UserName=" + queryUserName;
            return url;
        }

        protected void btnQuery_Click(object sender, EventArgs e)
        {
            Response.Redirect("UserList.aspx?UserName=" + custName.Text);
        }
    }
}
